package com.library.verification.manager;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.library.verification.view.CurrentPageDragVerificationView;
import com.library.verification.view.CurrentPageImageVerificationView;
import com.library.verification.view.VerificationListener;
import com.library.verification.view.DragVerificationView;
import com.library.verification.view.IBaseView;
import com.library.verification.view.IVerificationView;
import com.library.verification.window.CurrentPageWindow;
import com.library.verification.window.IWindow;
import com.library.verification.view.ImageVerificationView;
import com.library.verification.window.CommonPopWindow;


import java.util.List;

/**
 * Created by zhoulikai on 17/4/18.
 * 拖拽验证管理
 */

public class VerificationManager extends AbstractWindowManager implements VerificationListener {

    private Context context;
    private IVerificationView dragView;
    private VerificationListener listener;

    public VerificationManager(Context context, VerificationManagerParameter managerParameter) {
        super(context, managerParameter);
        this.context = context;
        dragView = (IVerificationView) getBaseView();

    }

    @Override

    protected IWindow makeWindowObj(Context context, IWindowParameter parameter) {
        if (parameter == null) {//默认弹窗
            return new CommonPopWindow(context);
        }
        if (parameter != null && parameter instanceof VerificationManagerParameter) {//类型
            if (((VerificationManagerParameter) parameter).showStyle() == VerificationManagerParameter.VERIFICATION_CURRENT_PAGE_STYLE) {
                return new CurrentPageWindow(context);
            } else if (((VerificationManagerParameter) parameter).showStyle() == VerificationManagerParameter.VERIFICATION_POP_STYLE) {
                return new CommonPopWindow(context);
            }
        }
        return new CommonPopWindow(context);
    }

    @Override
    protected IBaseView makeWindowView(Context context, IWindowParameter parameter) {
        if (parameter == null) {
            return new ImageVerificationView(context);
        }

        if (parameter instanceof VerificationManagerParameter) {
            if (((VerificationManagerParameter) parameter).getVerificationType() == VerificationManagerParameter.VERIFICATION_DRAG_TYPE) {
                if (((VerificationManagerParameter) parameter).showStyle() == VerificationManagerParameter.VERIFICATION_CURRENT_PAGE_STYLE) {
                    return new CurrentPageDragVerificationView(context);
                } else {
                    return new DragVerificationView(context);
                }

            } else if (((VerificationManagerParameter) parameter).getVerificationType() == VerificationManagerParameter.VERIFICATION_IMAGE_TYPE) {
                if (((VerificationManagerParameter) parameter).showStyle() == VerificationManagerParameter.VERIFICATION_CURRENT_PAGE_STYLE) {
                    CurrentPageImageVerificationView currentPageImageVerificationView = new CurrentPageImageVerificationView(context);
                    currentPageImageVerificationView.setVerificationManagerParameter((VerificationManagerParameter) parameter);
                    return currentPageImageVerificationView;
                } else {
                    return new ImageVerificationView(context);
                }

            }
        } else {
            return new ImageVerificationView(context);
        }
        return new ImageVerificationView(context);
    }

    public void show(ViewGroup parent, List<String> imageList, VerificationListener listener) {
        if (context == null) {
            return;
        }
        this.listener = listener;
        if (dragView != null) {
            dragView.setData(parent, imageList, this);
        }
        show(parent);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (dragView != null) {
            dragView.dismiss();
        }
    }

    @Override
    public void notifyStatusResult(int status) {
        if (status == SUCCESS_STATUS || status == CACEL_STATUS) {
            dismiss();
        }
        if (status != CACEL_STATUS) {
            if (this.listener != null) {
                this.listener.notifyStatusResult(status);
            }
        }


    }

    //验证码类型，目前有拖拽与图片验证两种类型，供给调用方调用,如不指定，默认为图片验证
    public interface VerificationManagerParameter extends IWindowParameter {

        public final static int VERIFICATION_DRAG_TYPE = 0;
        public final static int VERIFICATION_IMAGE_TYPE = 1;

        //弹窗样式
        public final static int VERIFICATION_POP_STYLE = 0;
        //当前页面显示
        public final static int VERIFICATION_CURRENT_PAGE_STYLE = 1;

        public int getVerificationType();

        //显示的样式
        public int showStyle();

        //是否内部提示
        public boolean isInsideTextTip();
    }


}
